package com.qingzhuge.member.service.impl;

import com.qingzhuge.common.utils.JsonUtil;
import com.qingzhuge.member.dao.SysMemberCloundAuthDao;
import com.qingzhuge.member.entity.SysMember;
import com.qingzhuge.member.entity.SysMemberVerification;
import com.qingzhuge.member.service.ISysMemberCloundAuthService;
import com.qingzhuge.service.BasePkLongServiceImpl;
import com.qingzhuge.verification.dto.CloudAuthRequest;
import com.qingzhuge.verification.dto.UserAuthRequest;
import com.qingzhuge.verification.dto.UserAuthResponse;
import com.qingzhuge.verification.dto.ali.DataInfo;
import com.qingzhuge.verification.enums.EnumCloudType;
import com.qingzhuge.verification.service.ISysVerificationService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author : zero.xiao
 * @description :
 * @date : 2018/8/8 0008 下午 16:57
 * @modified :
 * @since :
 */
@Service
public class SysMemberCloundAuthServiceImpl extends BasePkLongServiceImpl<SysMemberCloundAuthDao, SysMemberVerification> implements ISysMemberCloundAuthService {
    @Resource
    private ISysVerificationService verificationService;

    @Override
    public SysMemberVerification findByUser(Long userId) {
        return baseDao.findByUserId(userId);
    }

    @Override
    public SysMemberVerification findByToken(String token) {
        return baseDao.findByToken(token);
    }

    @Override
    public SysMemberVerification auth(CloudAuthRequest request, SysMember member){
        UserAuthResponse authResponse = verificationService.doAuth(request);
        SysMemberVerification cloundAuth = null;
        if(null != authResponse) {
            cloundAuth = new SysMemberVerification(request.getType(),request.getName(), request.getIdNumber());
            if (null != member) {
                SysMemberVerification dbMemberCloundAuth = this.findByUser(member.getId());
                if (null != dbMemberCloundAuth) {
                    cloundAuth.setId(dbMemberCloundAuth.getId());
                }
                cloundAuth.setName(request.getName());
                cloundAuth.setIdNumber(request.getIdNumber());
                cloundAuth.setUserId(member.getId());
            }
            cloundAuth.setToken(authResponse.getToken());
            if (EnumCloudType.aliyun.equals(request.getType())) {
                cloundAuth.setTicketId(authResponse.getTicketId());
            }else if(EnumCloudType.aliyunh5.equals(request.getType())){
                cloundAuth.setTicketId(authResponse.getTicketId());
                cloundAuth.setH5page(authResponse.getH5page());
            }else{
                cloundAuth.setTicketId(authResponse.getToken());
            }
            cloundAuth = super.save(cloundAuth);
        }
        return cloundAuth;
    }

    @Override
    public SysMemberVerification getAuth(String token){
        SysMemberVerification memberVerification = this.findByToken(token);
        if (null != memberVerification) {
            UserAuthResponse authresponse = verificationService.getAuth(new UserAuthRequest(memberVerification.getType(),memberVerification.getTicketId(), token));
            if (authresponse.getStatus()) {
                String json = authresponse.getJson();
                DataInfo data = JsonUtil.fromJson(json, DataInfo.class);
                BeanUtils.copyProperties(data,memberVerification);
                log.debug("实人认证通过");
            } else {
                memberVerification.setStatus(false);
                log.debug("实人认证未通过");
            }
            super.save(memberVerification);
        }
        return memberVerification;
    }
}
